package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.Version;
import com.api.shopapi.service.VersionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * app版本控制
 * */

@RestController
@RequestMapping("Version")
public class VersionController extends BaseController {

    @Autowired
    @Qualifier("VersionServiceImplOne")
    private VersionService versionService;


    //增加App版本
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Version/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String name = jsonObject.getString("name");
            String version = jsonObject.getString("version");
            String url = jsonObject.getString("url");
            Version versionEntity=new Version();
            versionEntity.setVersionCode(UUID.randomUUID().toString());
            versionEntity.setName(name);
            versionEntity.setVersion(version);
            versionEntity.setUrl(url);
            versionService.insert(versionEntity);
            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/Version/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //修改APP版本
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Version/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String versionCode = jsonObject.getString("versionCode");
            String name = jsonObject.getString("name");
            String version = jsonObject.getString("version");
            String url = jsonObject.getString("url");
            Version versionEntity = versionService.getByVersionCode(versionCode);
            versionEntity.setName(name);
            versionEntity.setVersion(version);
            versionEntity.setUrl(url);
            versionService.update(versionEntity);
            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/Version/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //删除APP版本
    @RequestMapping(value = "Delete",method = RequestMethod.POST)
    public MyResult Delete(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Version/Delete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String versionCode = jsonObject.getString("versionCode");
            versionService.delete(versionCode);
            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/Version/Delete", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //查询APP版本(app)
    @RequestMapping(value = "GetByName",method = RequestMethod.POST)
    public MyResult GetByName(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Version/GetByName");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String name = jsonObject.getString("name");
            Version version = versionService.getByName(name);
            return success(version);
        }catch (Exception e){
            insertError("/Version/GetByName", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //查询APP版本列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Version/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            String name = jsonObject.getString("name");
            String version = jsonObject.getString("version");
            PageInfo<Version> versionPageInfo = versionService.getPageList(pageIndex, pageSize, name, version);
            return success(versionPageInfo);
        }catch (Exception e){
            insertError("/Version/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }




}
